//
//  StarEvaluateView.m
//  yiluServeForUser
//
//  Created by yang yang on 2018/8/7.
//  Copyright © 2018年 Shanghai Tan Qi. All rights reserved.
//

#import "StarEvaluateView.h"

@interface StarEvaluateView ()

@property (nonatomic,strong) NSMutableArray * fullStarMarr , * emptyStarMarr;

@property (nonatomic,strong) UIView * fullBgView;

@property (nonatomic,strong) UIView * emptyBgView;

@property (nonatomic,assign) float width;

@end

@implementation StarEvaluateView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:(CGRect)frame];
    if (self) {
        [self layoutUI];
    }
    return self;
}


-(void)layoutUI{
    
    __weak typeof (self)weakSelf = self;
    CGFloat width = self.bounds.size.width/5;
    
    self.emptyBgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
    [self addSubview:self.emptyBgView];
    
    
    for (NSInteger i= 0; i < 5; i++) {
        UIImageView * emptyImageView = [UIImageView new];
        emptyImageView.image = [UIImage imageNamed:@"star_g"];
        emptyImageView.userInteractionEnabled = YES;
        [self.emptyBgView addSubview:emptyImageView];
        [self.emptyStarMarr addObject:emptyImageView];
        [emptyImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(weakSelf.emptyBgView);
            make.left.equalTo(weakSelf.emptyBgView.mas_left).offset(i*width);
            make.width.mas_equalTo(width);
        }];
    }
    self.fullBgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
    self.fullBgView.contentMode = UIViewContentModeLeft;
    self.fullBgView.clipsToBounds = YES;
    self.fullBgView.userInteractionEnabled = NO;
    [self addSubview:self.fullBgView];
    
    //单击手势
    UITapGestureRecognizer *tapGR=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGR:)];
    [self.emptyBgView addGestureRecognizer:tapGR];
    
    //拖动手势
    UIPanGestureRecognizer *panGR=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGR:)];
    [self.emptyBgView addGestureRecognizer:panGR];
    
    for (NSInteger i= 0; i < 5; i++) {
        UIImageView * fullImageView = [UIImageView new];
        fullImageView.image = [UIImage imageNamed:@"star_y"];
        fullImageView.userInteractionEnabled = YES;
        [self.fullBgView addSubview:fullImageView];
        [self.fullStarMarr addObject:fullImageView];
        [fullImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(weakSelf.fullBgView);
            make.left.equalTo(weakSelf.fullBgView.mas_left).offset(i*width);
            make.width.mas_equalTo(width);
        }];
    }
}

#pragma mark 手势
//单击手势
-(void)tapGR:(UITapGestureRecognizer *)tapGR{
    CGPoint tapPoint=[tapGR locationInView:self.fullBgView];
    CGFloat starWidthSelect = tapPoint.x;
    CGFloat starWidth = self.bounds.size.width/10;
    
    NSInteger levelStar = 0;
    if (starWidthSelect>0 && starWidthSelect <= starWidth) {//0-0.5
        _width = starWidth;
        levelStar = 0.5;
    }else if(starWidthSelect>starWidth && starWidthSelect <= starWidth*2){//0/5-1
        _width = starWidth*2;
        levelStar = 1;
    }else if(starWidthSelect>starWidth*2 && starWidthSelect <= starWidth*3){//1-1.5
        _width = starWidth*3;
        levelStar = 1.5;
    }else if(starWidthSelect>starWidth*3 && starWidthSelect <= starWidth*4){//1.5-2
        _width = starWidth*4;
        levelStar = 2;
    }else if(starWidthSelect>starWidth*4 && starWidthSelect <= starWidth*5){//2-2.5
        _width = starWidth*5;
        levelStar = 2.5;
    }else if(starWidthSelect>starWidth*5 && starWidthSelect <= starWidth*6){//2.5-3
        _width = starWidth*6;
        levelStar = 3;
    }else if(starWidthSelect>starWidth*6 && starWidthSelect <= starWidth*7){//3-3.5
        _width = starWidth*7;
        levelStar = 3.5;
    }else if(starWidthSelect>starWidth*7 && starWidthSelect <= starWidth*8){//3.5-4
        _width = starWidth*8;
        levelStar = 4;
    }else if(starWidthSelect>starWidth*8 && starWidthSelect <= starWidth*9){//4-4.5
        _width = starWidth*9;
        levelStar = 4.5;
    }else if(starWidthSelect>starWidth*9 && starWidthSelect <= starWidth*10){//4.5-5
        _width = starWidth*10;
        levelStar = 5;
    }else if (starWidthSelect < 0){
        levelStar = 0.5;
        _width = starWidth;
    }else{
        levelStar = 5;
        _width = starWidth*10;
    }
    
    self.fullBgView.frame=CGRectMake(0,0,_width,self.bounds.size.height);
    if (_delegate) {
        [_delegate theCurrentLevelWith:levelStar];
    }
}

-(void)configLevel:(NSInteger)level{
    
    CGFloat starWidth = (self.bounds.size.width/10)*level;
    self.fullBgView.frame=CGRectMake(0,0,starWidth,self.bounds.size.height);
}

//拖动手势
- (void)panGR:(UIPanGestureRecognizer *)panGR
{
    CGPoint panPoint = [panGR locationInView:self];
    if (panPoint.x<0||panPoint.x>self.bounds.size.width) {
        return;
    }
    CGFloat starWidthSelect = panPoint.x;
    CGFloat starWidth = self.bounds.size.width/10;
    NSInteger levelStar = 0;
    
    if (starWidthSelect>=0 && starWidthSelect <= starWidth) {//0-0.5
        _width = starWidth;
        levelStar = 0.5;
    }else if(starWidthSelect>starWidth && starWidthSelect <= starWidth*2){//0/5-1
        _width = starWidth*2;
        levelStar = 1;
    }else if(starWidthSelect>starWidth*2 && starWidthSelect <= starWidth*3){//1-1.5
        _width = starWidth*3;
        levelStar = 1.5;
    }else if(starWidthSelect>starWidth*3 && starWidthSelect <= starWidth*4){//1.5-2
        _width = starWidth*4;
        levelStar = 2;
    }else if(starWidthSelect>starWidth*4 && starWidthSelect <= starWidth*5){//2-2.5
        _width = starWidth*5;
        levelStar = 2.5;
    }else if(starWidthSelect>starWidth*5 && starWidthSelect <= starWidth*6){//2.5-3
        _width = starWidth*6;
        levelStar = 3;
    }else if(starWidthSelect>starWidth*6 && starWidthSelect <= starWidth*7){//3-3.5
        _width = starWidth*7;
        levelStar = 3.5;
    }else if(starWidthSelect>starWidth*7 && starWidthSelect <= starWidth*8){//3.5-4
        _width = starWidth*8;
        levelStar = 4;
    }else if(starWidthSelect>starWidth*8 && starWidthSelect <= starWidth*9){//4-4.5
        _width = starWidth*9;
        levelStar = 4.5;
    }else if(starWidthSelect>starWidth*9 && starWidthSelect <= starWidth*10){//4.5-5
        _width = starWidth*10;
        levelStar = 5;
    }else if (starWidthSelect < 0){
        levelStar = 0.5;
        _width = starWidth;
    }else{
        levelStar = 5;
        _width = starWidth*10;
    }
    
    self.fullBgView.frame=CGRectMake(0,0,_width,self.bounds.size.height);
    if (_delegate) {
        [_delegate theCurrentLevelWith:levelStar];
    }
}

-(void)setStarSpace:(CGFloat)starSpace{
    
    _starSpace = starSpace;
    __weak typeof (self)weakSelf = self;
    CGFloat width = self.bounds.size.width/5;
    for (NSInteger i = 0; i < self.fullStarMarr.count; i++) {
        UIImageView * imageView = self.fullStarMarr[i];
        [imageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(weakSelf.fullBgView);
            make.left.equalTo(weakSelf.fullBgView.mas_left).offset(i*width+starSpace);
            make.width.mas_equalTo(width-starSpace*2);
        }];
    }
    
    for (NSInteger i = 0; i < self.emptyStarMarr.count; i++) {
        UIImageView * imageView = self.emptyStarMarr[i];
        [imageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(weakSelf.emptyBgView);
            make.left.equalTo(weakSelf.emptyBgView.mas_left).offset(i*width+starSpace);
            make.width.mas_equalTo(width-starSpace*2);
        }];
    }
    
    
    
}

-(CGFloat)starSpace{
    return _starSpace;
}

-(void)setFullStarImage:(UIImage *)fullStarImage{
    
    _fullStarImage = fullStarImage;
    for (NSInteger i = 0; i < self.fullStarMarr.count; i++) {
        UIImageView * imageView = self.fullStarMarr[i];
        imageView.image = fullStarImage;
    }
    
}

-(UIImage *)fullStarImage{
    return _fullStarImage;
}

-(void)setEmptyStarImage:(UIImage *)emptyStarImage{
    
    _emptyStarImage = emptyStarImage;
    for (NSInteger i = 0; i < self.emptyStarMarr.count; i++) {
        UIImageView * imageView = self.emptyStarMarr[i];
        imageView.image = emptyStarImage;
    }
    
}

-(UIImage *)emptyStarImage{
    return _emptyStarImage;
}

-(NSMutableArray *)fullStarMarr{
    
    if (_fullStarMarr == nil) {
        _fullStarMarr = [[NSMutableArray alloc]initWithCapacity:0];
    }
    return _fullStarMarr;
}

-(NSMutableArray *)emptyStarMarr{
    
    if (_emptyStarMarr == nil) {
        _emptyStarMarr = [[NSMutableArray alloc]initWithCapacity:0];
    }
    return _emptyStarMarr;
}

@end
